package com.txx.service;

import com.txx.mapper.PermissionMapper;
import com.txx.pojo.Permission;
import com.txx.util.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class PermissionServiceImpl implements PermissionService  {

    @Autowired
    private PermissionMapper permissionMapper;

    /**
     * @author zgc
     *
     */
    @Override
    public List<Permission> findPermissionList() {
         // 获取所有数据
         List<Permission> allList  = permissionMapper.selectList(null);

         // 获取第一级的菜单
         List<Permission> oneList = allList.stream()
         .filter(e -> e.getLevel() == (Integer) Constants.PermissionLevel.One.ordinal())
         .collect(Collectors.toList());

         // 组装成两层
        List<Permission> list = new ArrayList<Permission>();
        for (Permission permission:  oneList) {
            permission.setChildren(allList.stream().filter(e -> e.getParentId() == permission.getId()).collect(Collectors.toList()));
            list.add(permission);
        }

        return list;
    }
}
